// import Cookies from 'js-cookie'
// import qs from 'qs'
// _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
// Please ASK KEN to REVIEW code change if you need MODIFY this file
// _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
// import moment from 'moment'
import moment from 'moment'
export default {

  data () {
    let year = new Date().getFullYear()
    return {
      mixinViewModuleOptions: {
        createdIsNeed: true,
        activatedIsNeed: false,
        pageId: 'report-view-module'
      },
      dataForm: {},
      dataList: [],
      order: '',
      orderField: '',
      page: 1,
      limit: 10,
      total: 0,
      loading: false,
      grid: null,
      reportSetting: null,
      dateRangeDefaultSetting: {
        format: 'yyyy-MM-dd',
        minDate: new Date('10/1/2019'),
        maxDate: new Date('1/1/2025'),
        weekStartDate: moment().subtract('days', 6).format(),
        weekEndDate: moment().subtract('days', 0).format(),
        monthStartDate: new Date(new Date(new Date().setDate(1)).toDateString()),
        monthEndDate: new Date(new Date(new Date(new Date().setMonth(new Date().getMonth() + 1)).setDate(0)).toDateString()),
        lastMonthStartDate: moment().subtract(1, 'months').date(1).format(),
        lastMonthEndDate: moment().date(0).format(),
        lastYearStartDate: new Date(year - 1, 1, 1).toDateString(),
        lastYearEndDate: new Date(year - 1, 11, 31).toDateString()
      },
      pivotTableDefaultSetting: {
        dataSourceSettings: {
          dataSource: this.dataList,
          values: null,
          formatSettings: null,
          columns: null,
          rows: null,
          filters: null,
          expandAll: false,
          enableSorting: true
        },
        showTooltip: false,
        enableValueSorting: true,
        gridSettings: { columnWidth: 140 },
        width: '100%',
        allowDrillThrough: true,
        allowExcelExport: true,
        allowConditionalFormatting: true,
        allowNumberFormatting: true,
        allowPdfExport: true,
        showToolbar: true,
        allowCalculatedField: true,
        showFieldList: true,
        displayOption: { view: 'Both' },
        chartSettings: {
          title: 'Sales Analysis',
          load: (args) => {
            let selectedTheme = location.hash.split('/')[1]
            selectedTheme = selectedTheme || 'Material'
            args.chart.theme = (selectedTheme.charAt(0).toUpperCase() +
                            selectedTheme.slice(1))
          }
        },
        toolbar: [
          'Grid',
          'Chart',
          'Export',
          'SubTotal',
          'GrandTotal',
          'Formatting',
          'FieldList'
        ],
        hyperlinkSettings: {
          cssClass: 'e-custom-class',
          showHyperlink: true,
          showRowHeaderHyperlink: false,
          showColumnHeaderHyperlink: false,
          showValueCellHyperlink: false,
          showSummaryCellHyperlink: false,
          headerText: undefined,
          conditionalSettings: [{
            measure: 'mrPercent',
            conditions: 'NotBetween',
            value1: 0,
            value2: 0
          },
          {
            measure: 'atPercent',
            conditions: 'NotBetween',
            value1: 0,
            value2: 0
          }
          ]
        }
      }
    }
  },
  created () {
    if (this.mixinViewModuleOptions.createdIsNeed) {
      this.resetDataForm()
    }
  },
  activated () {
    if (this.mixinViewModuleOptions.activatedIsNeed) {
      this.query()
    }
  },
  methods: {
    query () {
      this.loading = true
      var _self = this
      this.getDataListAPI().then(({ data: res }) => {
        _self.dataList = res.data
        _self.grid.dataSourceSettings.dataSource = this.dataList
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    resetDataForm () {},
    dataListSelectionChangeHandle (val) {
      this.dataListSelections = val
    },
    dataListSortChangeHandle (data) {
      if (!data.order || !data.prop) {
        this.order = ''
        this.orderField = ''
        return false
      }
      this.order = data.order.replace(/ending$/, '')
      this.orderField = data.prop.replace(/([A-Z])/g, '_$1').toLowerCase()
      this.query()
    },
    pageSizeChangeHandle (val) {
      this.page = 1
      this.limit = val
      this.query()
    },
    pageCurrentChangeHandle (val) {
      this.page = val
      this.query()
    },
    getDataListAPI () {

    },
    closeCurrentTab (data) {
      var tabName = this.$store.state.contentTabsActiveName
      this.$store.state.contentTabs = this.$store.state.contentTabs.filter(item => item.name !== tabName)
      if (this.$store.state.contentTabs.length <= 0) {
        this.$store.state.sidebarMenuActiveName = this.$store.state.contentTabsActiveName = 'home'
        return false
      }
      if (tabName === this.$store.state.contentTabsActiveName) {
        this.$router.push({ name: this.$store.state.contentTabs[this.$store.state.contentTabs.length - 1].name })
      }
    },
    exportHandle () {},
    beforeToolbarRender (args) {
      args.customToolbar.splice(6, 0, {
        type: 'Separator'
      })
      args.customToolbar.splice(9, 0, {
        type: 'Separator'
      })
    },
    saveReport (args) {
      // args.report.dataSourceSettings.dataSource = null
      this.reportSetting = args.report || null
    },
    fetchReport (args) {

    }

  }
}
